perf: simplify counter packing and dynamic truncation in generateOTP - #284
Open
pataar wants to merge 2 commits into
Open
perf: simplify counter packing and dynamic truncation in generateOTP#284pataar wants to merge 2 commits into
pataar wants to merge 2 commits into
Conversation
- Replace the hand-rolled intToByteString() with pack('J', $input).
'J' is unsigned 64-bit big-endian, which is exactly the counter
encoding required by RFC 4226. Drops a loop, an array, array_reverse,
implode, and str_pad per OTP generation. Byte-equivalent for all
inputs including 0, values > 2^32, and PHP_INT_MAX.
- Replace unpack('C*', $hash) + array_values with direct ord() reads on
the five bytes the truncation actually uses (last byte for the offset,
then four bytes at that offset). Removes the allocation of a ~20-entry
int array per generation.
Behavior is unchanged: both pieces produce byte-identical output to the
previous implementation. RFC 4226 and RFC 6238 Appendix B vector tests
in tests/HOTPTest.php and tests/TOTPTest.php cover the full pipeline
across SHA1, SHA256, and SHA512.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two byte-equivalent simplifications inside
OTP::generateOTP(). No behavior change.intToByteString()→pack('J', $input).Jis unsigned 64-bit big-endian — exactly the RFC 4226 counter encoding. Drops a loop, an array,array_reverse,implode, andstr_padper generation. The private helper is removed.unpack('C*', $hash) + array_values→ directord()reads on the five bytes the truncation actually uses (last byte for offset, then four at that offset). Removes a ~20-entry int array allocation per generation.Why it's safe
Verified byte-equivalence locally:
intToByteStringvspack('J')match for0, RFC timecodes, values > 2³², andPHP_INT_MAX; old vs new truncation match across 3000 hashes spanning SHA1/256/512. The RFC 6238 Appendix B vectors inTOTPTest::dataVectorsplus theat(0)edge case (where the oldwhile ($int !== 0)loop never iterated) exercise the full pipeline.Verified locally: PHPUnit (160 tests, 440 assertions), PHPStan, ECS, Rector, parallel-lint — all clean.